<template>
  <div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:degreetitle:add']" icon="el-icon-plus" plain size="mini" type="primary"
                   @click="handleAdd">添加主题
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="degreetitleList" border>
      <el-table-column align="center" label="问卷主题" prop="degreetitleName"/>
      <el-table-column align="center" label="问卷说明" prop="explain"/>
      <el-table-column align="center" label="创建人" prop="createName"/>
      <el-table-column align="center" label="创建时间" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.sendStatus == 0" v-hasPermi="['cld:degreetitle:survey']" size="mini" type="text"
                     @click="handleStartSurvey(scope.row)">发起调查
          </el-button>
          <el-button v-if="scope.row.sendStatus == 0" v-hasPermi="['cld:degreetitle:edit']" size="mini" type="text"
                     @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button v-if="scope.row.sendStatus == 0" v-hasPermi="['cld:degreetitle:remove']" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 添加或修改员工满意度主题对话框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="open" append-to-body title="添加主题" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
        <el-form-item label="问卷主题" prop="degreetitleName">
          <el-input v-model="form.degreetitleName" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="问卷说明" prop="explain">
          <el-input v-model="form.explain" placeholder="请输入" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 发起调查 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="surveyOpen" append-to-body title="发起调查" width="50%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
        <el-form-item label="添加项目" prop="projectType">
          <el-radio-group v-model="form.projectType">
            <el-radio label="1">所有项目</el-radio>
            <el-radio label="2">选择项目</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-row v-if="form.projectType == 2" :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button plain size="mini" type="primary" @click="handleAddProject">添加项目</el-button>
          </el-col>
          <el-table v-loading="loading" :data="form.projectList">
            <el-table-column align="center" label="项目名称" prop="projectName"/>
            <el-table-column align="center" label="项目简称" prop="projectName"/>
            <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
              <template slot-scope="scope">
                <el-button size="mini" type="text" @click="handleDeleteProject(scope.row)"
                >移除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormSurvey">确 定</el-button>
        <el-button @click="surveyOpen = false">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 选择项目 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="isProjectOpen" append-to-body title="选择项目"
               width="40%">
      <selected-project :notProjectIds="notProjectIds" @multipleInput="handleMultipleProject"></selected-project>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSelectedProject">确 定</el-button>
        <el-button @click="cancelOpenProject">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addDegreetitle,
  delDegreetitle,
  getDegreetitle,
  listDegreetitle,
  startSurvey,
  updateDegreetitle
} from "@/api/cld/degreetitle";
import selectedProject from "../project/selectedProject.vue";

export default {
  name: "Degreetitle",
  components: {selectedProject},
  props: {
    type: {
      type: Number
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 员工满意度主题表格数据
      degreetitleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 发起调查弹出层
      surveyOpen: false,
      // 项目选择弹框
      isProjectOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        degreetitleType: this.type
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        degreetitleName: [
          {required: true, message: "问卷主题不能为空", trigger: "blur"}
        ],
        explain: [
          {required: true, message: "问卷说明不能为空", trigger: "blur"}
        ],
      },
      // 已选择项目
      notProjectIds: [],
      // 临时存放选择项目
      temporaryList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询员工满意度主题列表 */
    getList() {
      this.loading = true;
      listDegreetitle(this.queryParams).then(response => {
        this.degreetitleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        degreetitleId: null,
        degreetitleName: null,
        explain: null,
        sendStatus: null,
        sendTime: null,
        degreetitleType: this.type,
        projectType: 1,
        projectList: []
      };
      this.resetForm("form");
    },
    // 发起调查
    handleStartSurvey(row) {
      this.reset();
      getDegreetitle(row.degreetitleId).then(response => {
        this.form = response.data;
        this.surveyOpen = true;
        this.title = "发起调查";
      });

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工满意度主题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getDegreetitle(row.degreetitleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工满意度主题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.degreetitleId != null) {
            updateDegreetitle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDegreetitle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 发起调查
    submitFormSurvey() {
      if (this.form.projectType == 1) {
        this.form.projectList = [];
      }
      startSurvey(this.form).then(res => {
        this.surveyOpen = false;
        this.getList();
        this.$modal.msgSuccess("操作成功");
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const degreetitleIds = row.degreetitleId || this.ids;
      this.$modal.confirm('是否确认删除员工满意度主题编号为"' + degreetitleIds + '"的数据项？').then(function () {
        return delDegreetitle(degreetitleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 选择项目
    handleAddProject() {
      this.isProjectOpen = true;
    },
    // 勾选项目回调
    handleMultipleProject(selection) {
      this.temporaryList = selection;
    },
    // 确认选择项目
    submitSelectedProject() {
      this.form.projectList = [];
      this.temporaryList.forEach(item => {
        this.form.projectList.push({
          ...item,
          degreetitleId: this.form.degreetitleId
        });
      })
      this.isProjectOpen = false;
      this.temporaryList = [];
    },
    // 取消选择项目
    cancelOpenProject() {
      this.isProjectOpen = false;
      this.temporaryList = [];
    },
    // 移除项目
    handleDeleteProject(row) {
      const index = this.form.projectList.indexOf(row);
      if (index > -1) {
        this.form.projectList.splice(index, 1);
      }
    }
  }
};
</script>
